package com.mla.fastdfs.utils;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ToolUtil {
    /**
     * 获取访问ip
     *
     * @param request
     * @return
     */
    public static String getLocalIp(HttpServletRequest request) {
        String remoteAddr = request.getRemoteAddr();
        String forwarded = request.getHeader("X-Forwarded-For");
        String realIp = request.getHeader("X-Real-IP");

        String ip = null;
        if (realIp == null) {
            if (forwarded == null) {
                ip = remoteAddr;
            } else {
                ip = remoteAddr + "/" + forwarded.split(",")[0];
            }
        } else {
            if (realIp.equals(forwarded)) {
                ip = realIp;
            } else {
                if (forwarded != null) {
                    forwarded = forwarded.split(",")[0];
                }
                ip = realIp + "/" + forwarded;
            }
        }
        return ip;
    }

    /**
     * 获取随机n位纯数字
     *
     * @param num
     * @return
     */
    public static String randomInt(int num) {
        Double random = Math.random();
        return random.toString().substring(2, 2 + num);
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            System.out.println(getOrderId(1));
            System.out.println(getOrderId(2));
        }
    }

    /**
     * 防重复
     *
     * @param supplier
     * @return
     */
    public static synchronized String getOrderId(Integer supplier) {
        StringBuilder orderId;
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssS");
        orderId = new StringBuilder(format.format(new Date()));
        while (orderId.length() < 17) {
            orderId.append("0");
        }
        orderId.append(getBus(supplier).toString());
        orderId.append(randomInt(3));
        return orderId.toString();
    }


    public static Integer getBus(Integer supplier) {
        int a = 0;
        switch (supplier) {
            case 1:
                a = 1001;
                break;
            case 2:
                a = 2001;
                break;
            case 3:
                a = 3001;
                break;
            default:

        }
        return a;
    }


    public static Integer getBusInt(String supplier) {
        int a = 0;
        switch (supplier) {
            case "1001":
                a = 1;
                break;
            case "2001":
                a = 2;
                break;
            case "3001":
                a = 3;
                break;
            default:

        }
        return a;
    }

    public static String getErrorLog(Exception e) {
        StringWriter sw = null;
        PrintWriter pw = null;
        try {
            sw = new StringWriter();
            pw = new PrintWriter(sw);
            // 将出错的栈信息输出到printWriter中
            e.printStackTrace(pw);
            pw.flush();
            sw.flush();
        } finally {
            if (sw != null) {
                try {
                    sw.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (pw != null) {
                pw.close();
            }
        }
        return sw.toString();
    }

}
